<template>
  <div class="roleManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form
          :inline="true"
          :model="queryForm.queryBean"
          @submit.native.prevent
        >
          <el-form-item>
            <el-input
              v-model.trim="queryForm.queryBean.userName"
              placeholder="用户名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.queryBean.roleId"
              placeholder="用户权限"
            >
              <el-option label="管理员" value="4"></el-option>
              <el-option label="游客" value="1"></el-option>
              <el-option label="会员" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :height="height"
      :element-loading-text="elementLoadingText"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="queryBean.userId"
        label="用户ID"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="userName"
        label="用户名称"
        width="100"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="头像">
        <template #default="{ row }">
          <el-image
            v-if="imgShow"
            :preview-src-list="imageList"
            :src="row.headerUrl"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="手机号码"
        prop="phone"
        width="120"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="用户类型"
        :formatter="formatType"
        prop="roleId"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <role-authority ref="edit" @fetch-data="fetchData"></role-authority>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
  import { getUserList } from '@/api/user'
  import RoleAuthority from './components/RoleAuthority'

  export default {
    name: 'Authority',
    components: { RoleAuthority },
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          0: 'info',
          2: 'error',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        imgShow: true,
        imageList: [],
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatType(row, column) {
        const userType = row.roleId
        switch (userType) {
          case '1':
            return '游客'
          case '2':
            return '会员'
          case '5':
            return '管理员'
        }
      },
      handleEdit(row) {
        console.log(row)
        if (row.userId) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getUserList(this.queryForm)
        const { body, totalSize } = data
        console.log(body.data)
        this.list = body.data
        // data.forEach((item, index) => {})
        this.total = totalSize
        this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
